﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hanhud.Web.UI;
using System.Text;

namespace Hanhud.Web.Controls
{
    public partial class Support : BaseUserControl
    {
        private EControlPosition _position = EControlPosition.Right;
        public EControlPosition Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public Support()
        {
            this.Init += new EventHandler(Support_Init);
            this.Load += new EventHandler(Support_Load);
        }

        void Support_Init(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jQuery.jScroll", Page.ResolveUrl("~/Scripts/jQuery.jScroll.js"));
        }

        void Support_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Page.IsCallback)
            {
                rptDepartment.DataSource = CSupport.GetDepartments();
                rptDepartment.DataBind();
            }
        }

        protected void rptDepartment_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){

                Repeater rptSupport = e.Item.FindControl("rptSupport") as Repeater;
                CDepartment current = (CDepartment)e.Item.DataItem;

                if (rptSupport != null)
                {
                    rptSupport.DataSource = CSupport.GetSupports(current.DepartmentID);
                    rptSupport.DataBind();
                }
            }
        }

        protected string GetNickType(object item)
        {
            CSupport current = (CSupport)item;

            // Lay danh sach nick
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < current.Nicks.Count; i++)
            {
                CNick nick = current.Nicks[i];

                sb.AppendFormat(@"<a href=""{4}"" style=""right: {3}px;"" class=""{0}"" title=""{1}""><em>{2}</em></a>",
                    nick.NickType == ENickType.Yahoo ? "yahoo" : (nick.NickType == ENickType.Skype ? "skype" : (nick.NickType == ENickType.Live ? "live" : "gtalk")),
                    "Hỗ trợ trực tuyến",
                    nick.NickName,
                    21 * i,
                    "javascript:void(0)");
            }
            return sb.ToString();
        }
    }

    public enum EControlPosition
    {
        Left,
        Right
    }
}